/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.AreaDAO;
import com.cashmate.hibernate.dao.BorrowerDAO;
import com.cashmate.hibernate.pojo.Areas;
import com.cashmate.hibernate.pojo.Borrowers;
import com.opensymphony.xwork2.ActionSupport;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Dine
 */

@Action(value = "/get-bname", results = {
    @Result(name = "success", type = "stream", params = {"inputName", "inputStream", "contentType", "text/html"}),
    @Result(name = "error", location = "WEB-INF/error/indexError.html")
})
public class GetBorrowerName extends ActionSupport {

    private static Logger logger = Logger.getLogger(GetBorrowerName.class);
    private BorrowerDAO bo = (BorrowerDAO) ServletActionContext.getServletContext().getAttribute("borrowerDAO");
    private AreaDAO ao = (AreaDAO) ServletActionContext.getServletContext().getAttribute("areaDAO");
    
    public int bcode, areaid;
    private InputStream inputStream;
    public String acodecoll, acode;
    private Areas area;
    private Borrowers bor;

    public void setBcode(int bcode) {
        this.bcode = bcode;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setAcode(String acodecoll) {
        this.acodecoll = acodecoll;
    }

    public void setAcodeColl(String acode) {
        this.acode = acode;
    }

    @Override
    public String execute() {

        try {
            
            area = ao.getByAreaCode(acodecoll);
            areaid = area.getAreaid();
          
            bo.getBorrowersPerArea(areaid);
            if (bo.getBorrowersPerArea(areaid).containsKey(bcode)) {
                bor = bo.getBorrowersPerArea(areaid).get(bcode);
                String name = bor.getFirstlastname();
                inputStream = new StringBufferInputStream(name);
            } else {
                inputStream = new StringBufferInputStream("exist");
            }


        } catch (Exception e) {
        }
        return SUCCESS;
    }
}
